﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using Common;

public partial class membership_profile : MemberPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "init":
                InitializeObj();
                break;
            case "save":
                Save();
                break;
        }
    }
    
    /// <summary>
    /// 获取送货地址信息
    /// </summary>
    void InitializeObj()
    {
        Model.MembershipBase _obj = this.UserInfo;
        StringBuilder GetValues = new StringBuilder();
        GetValues.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><Membership><fileds>");
        GetValues.Append("<filed name=\"FullName\">" + _obj.FullName + "</filed>");
        GetValues.Append("<filed name=\"BirthDay\">" + _obj.Birthday.ToString("yyyy-MM-dd").Replace("1754-01-01", "") + "</filed>");
        GetValues.Append("<filed name=\"Gender\">" + _obj.Gender.ToString() + "</filed>");
        GetValues.Append("<filed name=\"Address\">" + _obj.Address + "</filed>");
        GetValues.Append("<filed name=\"Tel\">" + _obj.Tel + "</filed>");
        GetValues.Append("<filed name=\"ZIP\">" + _obj.ZIP + "</filed>");
        GetValues.Append("<filed name=\"Mobile\">" + _obj.Mobile + "</filed>");
        GetValues.Append("<filed name=\"Email\">" + _obj.Email + "</filed>");
        GetValues.Append("<filed name=\"ProvinceId\">" + _obj.ProvinceId.ToString() + "</filed>");
        GetValues.Append("<filed name=\"CityId\">" + _obj.CityId.ToString() + "</filed>");
        GetValues.Append("<filed name=\"CountieId\">" + _obj.CountieId.ToString() + "</filed>");

        GetValues.Append("<filed name=\"Username\">" + _obj.UserName + "</filed>");
        GetValues.Append("<filed name=\"MobileAlias\">" + _obj.MobileAlias + "</filed>");
        GetValues.Append("<filed name=\"RegionInfo\">" + _obj.RegionInfo + "</filed>");
        GetValues.Append("<filed name=\"MobilePIN\">" + _obj.MobilePIN + "</filed>");
        GetValues.Append("<filed name=\"IsApproved\">" + _obj.IsApproved.ToString() + "</filed>");
        GetValues.Append("<filed name=\"IsLockedOut\">" + _obj.IsLockedOut.ToString() + "</filed>");
        GetValues.Append("</fileds></Membership>");

        BLL.AjaxHelper.ResponseWriteXML(GetValues.ToString());
    }

    Model.MembershipBase UserObj
    {
        get
        {
            Model.MembershipBase _userObj = new Model.MembershipBase();
 
            _userObj.UserId = this.UserId;
            _userObj.UserName = RequestBase.GetFormString("txtUsername");
            _userObj.FullName = RequestBase.GetFormString("txtFullName");
            _userObj.MobileAlias = RequestBase.GetFormString("txtMobileAlias");
            _userObj.Birthday = DateTime.Parse(string.IsNullOrEmpty(RequestBase.GetFormString("txtBirthday")) ? "1754-01-01" : RequestBase.GetFormString("txtBirthday"));
            _userObj.Gender = int.Parse(RequestBase.GetFormString("selGender"));
            _userObj.CountryId = 0;
            _userObj.ProvinceId = int.Parse(string.IsNullOrEmpty(RequestBase.GetFormString("txtProvinceId")) ? "0" : RequestBase.GetFormString("txtProvinceId"));
            _userObj.CityId = int.Parse(string.IsNullOrEmpty(RequestBase.GetFormString("txtCityId") ) ? "0" : RequestBase.GetFormString("txtCityId"));
            _userObj.CountieId = int.Parse(string.IsNullOrEmpty(RequestBase.GetFormString("txtCountieId") ) ? "0" : RequestBase.GetFormString("txtCountieId"));
            _userObj.RegionInfo = RequestBase.GetFormString("txtRegionInfo");
            _userObj.RegionId = int.Parse(string.IsNullOrEmpty(RequestBase.GetFormString("txtCountieId")) ? "0" : RequestBase.GetFormString("txtCountieId"));
            _userObj.MobilePIN = RequestBase.GetFormString("txtMobilePIN");
            _userObj.Email = RequestBase.GetFormString("txtEmail"); 
            _userObj.PasswordQuestion = "";
            _userObj.PasswordAnswer = "";
            _userObj.IsApproved = bool.Parse(RequestBase.GetFormString("txtIsApproved"));
            _userObj.IsLockedOut = bool.Parse(RequestBase.GetFormString("txtIsLockedOut"));
            _userObj.Address = RequestBase.GetFormString("txtAddress");
            _userObj.Tel = RequestBase.GetFormString("txtTel");
            _userObj.ZIP = RequestBase.GetFormString("txtZIP");
            _userObj.Mobile = RequestBase.GetFormString("txtMobile");
            _userObj.Comment = "";

            return _userObj;
        }
    }

    #region 保存数据
    void Save()
    {
        Model.Enum.MembershipCreateStatus status = Model.Enum.MembershipCreateStatus.ProviderError;
        Model.MembershipBase _obj = (new BLL.MembershipBase()).UpdateUser(UserObj, out status);

        switch ((int)status)
        {
            case 0:
                BLL.AjaxHelper.ResponseWrite("succeeded");
                break;
            default:
                BLL.AjaxHelper.ResponseWrite(status.ToString());
                break;
        }
    }
    #endregion
}